package org.layui.servlet;

import org.layui.dao.UserDao;
import org.layui.dao.impl.UserDaoImpl;
import org.layui.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 修改用户
 */
@WebServlet("/api/user/update")
public class UserUpdateServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取当前页和每页显示的记录数
        int id = Integer.parseInt(req.getParameter("id"));
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String zipcode = req.getParameter("zipcode");
        String level = req.getParameter("level");

        // 定义实体对象，并封装数据
        User user = new User() ;
        user.setId(id);
        user.setSex(sex);
        user.setPhone(phone);
        user.setAddress(address);
        user.setZipCode(zipcode);
        user.setLevel(level);

        // 创建DAO对象，实现数据的查询（多条件+分页）
        UserDao userDao = new UserDaoImpl();
        int r = userDao.update(user) ;
        if(r==1) {
            // 把分页的信息，封装到PageVO对象中，并返回到客户端
            print(resp, successJson("修改成功"));
            return ;
        }
        print(resp, errorJson("修改失败"));

    }
}
